package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/images")
public class ImageController {
    /**
     * 上传服务器，并返回指定格式数据
     * @param upload
     * @return upload
     */
    @PostMapping
    public Map<String, Object> upload(MultipartFile upload) {
        String imgUrl = null;
        Map<String, Object> map = new HashMap<>();
        try {
            imgUrl = UploadUtil.uploadQiniu(upload);
            map.put("uploaded", 1);
            map.put("url", imgUrl);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("uploaded", 0);
            HashMap<Object, Object> temp = new HashMap<>();
            temp.put("message", e.getMessage());
            map.put("error", temp);
        }
        return map;
    }
}
